package org.dizem.groupon.dao.impl;

import org.dizem.groupon.dao.CityDAO;
import org.dizem.groupon.model.domain.CityEntity;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import java.util.List;

/**
 * User: dizem@126.com
 * Time: 11-6-21 上午10:43
 */
public class CityDAOImpl extends DAOImplSupport<CityEntity> implements CityDAO {

	public CityEntity getById(int id) {
		return (CityEntity) getHibernateTemplate().get(CityEntity.class, id);
	}

	public List<CityEntity> loadAll() {
		return getHibernateTemplate().loadAll(CityEntity.class);
	}

	public List<CityEntity> load(int firstResult, int maxResult) {
		return getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(CityEntity.class), firstResult, maxResult);
	}

	public CityEntity getByChnName(String chnName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(CityEntity.class);
		criteria.add(Restrictions.eq("chnName", chnName));
		List result = getHibernateTemplate().findByCriteria(criteria);
		return result.size() == 0 ? null : (CityEntity) (result.get(0));
	}
}
